<?php
/**
 * Created by PhpStorm.
 * User: 冯轲
 * Date: 2017-12-13
 * Time: 11:59
 */
namespace app\home\controller;

use app\admin\model\User;
use think\Controller;
use think\Db;
use think\Request;
use think\Session;
use ucpaas\Ucpaas;

class Checkout extends Controller
{
    //购物车结算页面
    public function getindex()
    {
        //通过当前用户的id来进行判断
        $uid=Session::get('userinfo')['user_id'];
        //print_r($uid);
        //通过id得到用户的购物车信息
        $cart_id=Db::name('cart')->where('uid',$uid)->value('id');
        $good = Db::name('cart_goods')
            ->alias('cg')
            ->join('cart c', 'cg.cart_id=c.id')
            ->join('goods g', 'cg.goods_id=g.id')
            ->join('models m','cg.goods_model=m.id')
            ->where('c.uid', $uid)
            ->select();
        $this->assign('data',$good);
        //print_r($cart_id);
        //收货地址
        $address = Db::name('address')->where('uid',$uid)->select();
        $this->assign('address',$address);
        //总得金额价格计算
//        print_r($good);
        $total=0;
        foreach ($good as $val){
            $total += $val['cart_num']*$val['goods_price'];
        }
        $total=number_format($total,2);
        $this->assign('total',$total);
        return $this->fetch('checkout/index');
    }
    //添加收货地址页面
    public function getaddress(){
        return $this->fetch('/checkout/address');
    }
    //添加收货地址保存
    public function postAddress(Request $request){
        $uid=Session::get('userinfo')['user_id'];
        $address=$request->except('action');
        $address['uid']=$uid;
        $address['status']=0;
        if($address['address_address']==null){
            $data=[
                'status' => 2,
                'msg' => '请选择城市',
            ];
            return json($data);
        }
        $re=Db::name('address')->insert($address);
        if($re){
            //返回数据，进行判断是否添加成功
            $data=[
                'status' => 0,
                'msg' => '收货地址添加成功',
            ];
        }else{
            $data=[
                'status' => 1,
                'msg' => '收货地址添加失败',
            ];
        };
        return json($data);
    }
    //获取城市信息
    public function getCity($upid){
        $city=Db::name('district')->where('upid',$upid)->select();

        return $city;
    }
    //提交订单
    public function postOrders(Request $request){
        $uid=Session::get('userinfo')['user_id'];
        $orders=$request->except('action');
        $orders['create_time']=time();
        $orders['status']=0;
        $orders['user_id']=$uid;
        $cart_id=$good=Db::name('cart')->where('uid',$uid)->value('id');
        /*print_r($orders);
        print_r($goods_id);
        exit;*/
        if(Db::name('orders')->insert($orders)){
            //创建详情表
            $order_id=Db::name('orders')->getLastInsID();
            $goods=$good=Db::name('cart_goods')
                        ->alias('cg')
                        ->join('cart c','c.id=cg.cart_id')
                        ->where('cart_id',$cart_id)
                        ->select();
            foreach ($goods as $val){
                $data['orders_id']=$order_id;
                $data['goods_id']=$val['goods_id'];
                $data['goods_num']=$val['cart_num'];
                $data['goods_model']=$val['goods_model'];
                $ress=Db::name('orders_detail')->insert($data);
            }
            if(Db::name('cart')->where('uid',$uid)->delete()){
                Db::name('cart_goods')->where('cart_id',$cart_id)->delete();
            }
            //重定向跳转到支付界面
            return $this->redirect('/payment/index',['order_id'=>$order_id]);
            //echo '到达支付页面';
        }else{
            echo '请求失败，重新提交';
        }
    }

}